<?php
/************************************/
// Home controller
// thaodx@gmail.com
/************************************/

if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends CI_Controller {
	public $layout_view = 'layout/default';
	public function index(){
		$this->load->helper('url');
		if(!$this->session->userdata('id')){
			redirect($this->config->base_url.'/home/login', 'location', 301);
		}
		$this->layout->title('Dashboard | ');
		$this->layout->view('home_index', $data, 'layout/default');// Render view and layout
	}
	public function login(){
		if(isset($_POST['username']) && isset($_POST['password'])){
			$this->load->helper('str');
			$this->load->model('User_model', 'user');
			$username = cleanValue($_POST['username']);
			$password = md5($_POST['password']);

			$userData = $this->user->checkUserLogin($username, $password);
			// login successful
			if(!empty($userData)){
				$admin = array(
					'id' => $userData->admin_id,
					'username' => $userData->admin_username,
					'name' => $userData->admin_name,
					'lastlogin' => $userData->admin_lastlogin
					);
				$this->session->set_userdata($admin);
				$this->load->helper('url');
				redirect($this->config->base_url.'/home/index', 'location', 301);
			}
			else{
				$data['error'] = true;
			}
		}
		$this->layout->view('home_login', $data, 'layout/login');// Render view and layout
	}
}